<template>
	<view class="layout">
		<uni-header :title="'我的笔记'"></uni-header>
		<view class="content">
			<view class="note" v-for="item in noteData">
				<view class="note-head">
					<view class="note-left">
						<view class="note-name">{{item.note_name}}</view>
						<view class="note-time">{{item.rec_time}}</view>
					</view>
					<view class="note-right">{{item.types}}</view>
				</view>
				<view class="note-foot">
					<u-button type="primary" @click="editClick(item)" :plain="true" :hairline="true"
						icon="edit-pen-fill" text="修改"></u-button>
					<u-button type="primary" @click="viewClick(item)" :plain="true" :hairline="true" icon="eye-fill"
						text="详情"></u-button>
					<u-button type="primary" @click="shareNoteclick(item)" :plain="true" :hairline="true"
						icon="share-fill" text="分享"></u-button>
					<u-button type="error" @click="deleteClick(item)" :plain="true" :hairline="true" icon="trash-fill"
						text="删除"></u-button>
				</view>
			</view>
			<view class="add-note" @click="addClick"><u-icon name="plus-circle-fill" color="#2979ff" size="50"></u-icon>
			</view>
			<uni-popup ref="alertDialog" type="dialog">
				<uni-popup-dialog :type="msgType" cancelText="取消" confirmText="确定" title="通知" content="确定要删除该笔记吗？？"
					@confirm="dialogConfirm" @close="dialogClose"></uni-popup-dialog>
			</uni-popup>
			<uni-popup ref="share" type="dialog" backgroundColor="#fff">
				<uni-popup-dialog ref="shareClose" title="请选择分享的圈子" @confirm="shareSubmit">
					<uni-data-checkbox v-model="shareId" :localdata="circleData"></uni-data-checkbox>
				</uni-popup-dialog>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	import {
		getNote,
		deleteNote
	} from '@/api/note'
	import {
		getCircle
	} from "@/api/work"
	import {
		shareNote
	} from "@/api/mine"

	export default {
		data() {
			return {
				type: 'center',
				msgType: 'error',
				content: "00000",
				show: false,
				noteData: [],
				circleData: [],
				shareId: "",
				noteId: "",
				deleteData: {
					id: "",
					type: "",
					deviceId: this.$store.state.user.deviceId
				}
			}
		},
		created() {
			this.getNoteData();
		},
		methods: {
			getNoteData() {
				getNote({
					deviceId: this.$store.state.user.deviceId
				}).then(res => {
					res.recordList.forEach(item => {
						if (item.type == "1") {
							item["types"] = "常规教研笔记"
						} else if (item.type == "2") {
							item["types"] = "观摩课笔记"
						} else if (item.type == "3") {
							item["types"] = "主题教研笔记"
						} else if (item.type == "4") {
							item["types"] = "默认笔记"
						}
					})
					this.noteData = res.recordList;
				})
			},
			handleToInfo() {
				this.$tab.navigateTo('/pages/mine/info/index')
			},
			editClick(item) {
				this.$tab.navigateTo('/pages/mine/note/edit?id=' + item.id + '&type=' + item.type)
			},
			addClick() {
				this.$tab.navigateTo('/pages/mine/note/add')
			},
			viewClick(item) {
				this.$tab.navigateTo('/pages/mine/note/details?id=' + item.id + '&type=' + item.type)
			},
			deleteClick(item) {
				this.$refs.alertDialog.open();
				this.deleteData.id = item.id;
				this.deleteData.type = item.type;
			},

			shareNoteclick(data) {
				this.noteId = data.id
				this.type = data.type
				getCircle({
					deviceId: this.$store.state.user.deviceId
				}).then(res => {
					res.list.forEach(item => {
						this.circleData.push({
							value: item.id,
							text: item.title
						})
					});
					this.$refs.share.open()
				})
			},
			shareSubmit() {
				shareNote({
					noteId: this.noteId,
					type: this.type,
					circleId: this.shareId,
					deviceId: this.$store.state.user.deviceId
				}).then(res => {
					uni.$u.toast('分享成功');
				})
			},
			dialogConfirm() {
				deleteNote(this.deleteData).then(res => {
					if (res.status == 0) {
						uni.$u.toast('删除成功');
						this.getNoteData();
					}
				})
			},
			dialogClose() {
				console.log('点击关闭')
			},
			close() {
				this.show = false
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f5f6f7;

		.content {
			padding: 10px 15px;

			.note {
				background: #fff;
				padding: 10px;
				border-radius: 10px;
				margin-bottom: 10px;

				.note-head {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.note-left {
						.note-name {
							font-size: 24px;
							color: #2f2f2f;
						}

						.note-time {
							font-size: 16px;
							color: #666;
							margin-top: 10px;
						}
					}

					.note-right {
						font-size: 20px;
						color: #999;
					}
				}

				.note-foot {
					border-top: 1px solid #eee;
					margin-top: 5px;
					padding-top: 5px;
					display: flex;

					.note-option {
						font-size: 14px;
						margin-right: 20px;
						display: flex;
						align-items: center;
					}

					.edit {
						color: #3c96f3;
					}

					.delete {
						color: #a54444;
					}

					.u-button {
						margin: 0 5px;
					}
				}
			}

			.add-note {
				position: fixed;
				bottom: 10vh;
				right: 5vw;
			}
		}
	}
</style>